<template>
  <webview v-if="name" v-on:load-commit='loadCommit($event)' v-on:did-finish-load='didFinishLoad($event)' v-on:console-message="consoleMessage($event)" v-on:did-start-loading="startLoading($event)" v-on:did-stop-loading="stopLoading($event)" class="webview" id="hideAlyx" disablewebsecurity :src='loadUrl' style="width:100px;height:100px;position:fixed;z-index:99999999999999;left:-200px"></webview>
</template>

<script>
import { globalBus } from "@/assets/js/globalBus.js";
import { ut } from "@/assets/js/webViewAutoLogin.js";
const nedb = require("nedb");
const webUserBindDb = new nedb({
  filename: "/data/web-user-bind.db",
  autoload: true,
});
export default {
  data() {
    return {
      loginUrl: "https://qiye.aliyun.com/alimail/auth/login",
      loadUrl: "https://qiye.aliyun.com/alimail/",
      name: "",
      password: "",
      loginTag: 0, //自动登录进度 0未开始 1进行中 2验证通过 3失败
      operation: {
        formUrl: '',
        needUrl: ''
      },
    };
  },
  mounted() {
    var _this = this;
    webUserBindDb.find(
      {
        url: "https://qiye.aliyun.com/alimail/",
      },
      function (err, docs) {
        _this.bindUserList = docs;
        if (docs.length > 0) {
          _this.name = docs[0].name;
          _this.password = docs[0].password;
          for (var i = 0; i < docs.length; i++) {
            if (docs[i].use) {
              _this.name = docs[i].name;
              _this.password = docs[i].password;
              break;
            }
          }
          //一分钟获取一次
          setInterval(() => {
            if (_this.loginTag == 2) {
              document.getElementById("hideAlyx").loadURL(_this.loadUrl)
            }
          }, 60 * 1000);
        } else {
          globalBus.$emit("updateAlyxStatus", {
            code: -2,
            txt: "未绑定账号",
          });
        }
      }
    );

  },
  methods: {
    //读取未读消息
    readMsg() {
      var webView = document.getElementById("hideAlyx");

      if (webView.src.indexOf(this.loadUrl) < 0) {
        return;
      }

      var script = `var evt = document.createEvent("HTMLEvents");
        evt.initEvent("input", true, true);
        setTimeout(function(){
          var span=document.querySelector(".mail_tree_node_unread");
          if(span){
            var info=JSON.stringify({
              type:'alyx_unread',
              msgNum: span.innerText
            })
            console.log("awork::"+info)
          }else if(document.querySelector(".navpane_mail_fav_title")){
            var info=JSON.stringify({
              type:'alyx_unread',
              msgNum: 0
            })
            console.log("awork::"+info)
          }
        },500)
        `;
      webView.executeJavaScript(script);
    },
    loadCommit(e) {
      if (this.loginTag > 1 || !e.url) {
        return;
      }

      if (this.loginTag == 0 && e.url.indexOf(this.loginUrl) > -1) {
        this.loginTag = 1;
        ut.verifyNeedTo(e.url, document.getElementById("hideAlyx"));
        return;
      }

      if (this.loginTag == 1 && e.url.indexOf(this.loginUrl) > -1) {
        this.loginTag = 3;
        return;
      }

      if (this.loginTag == 1 && e.url.indexOf(this.loadUrl) > -1) {
        this.loginTag = 2;
        return;
      }
    },
    consoleMessage(e) {
      var msg = e.message;
      if (msg.indexOf("awork::") != 0) {
        return;
      }
      console.log(msg)
      msg = msg.replace("awork::", "");
      msg = JSON.parse(msg);
      if (msg.type == "alyx_unread") {
        globalBus.$emit("updateAlyxStatus", {
          code: 1,
          txt: "已登录",
          num: msg.msgNum,
        });
      }
    },
    startLoading(e) { },
    //加载完成时间
    stopLoading(e) {
      var src = document.getElementById("hideAlyx").getAttribute("src");
      //自动登录
      var dest = ut.verifySuport(src);
      if (dest && this.name) {
        dest.name = this.name;
        dest.password = this.password;
        console.log(dest)
        ut.aotuLogin(dest, document.getElementById("hideAlyx"));
      }

      if (this.loginTag == 2) {
        this.readMsg();
      } else if (this.loginTag == 3) {
        globalBus.$emit("updateAlyxStatus", {
          code: -1,
          txt: "登陆失败，点击重试",
        });
      }
    },
    didFinishLoad(e) { },
  },
};
</script>

<style lang="less">
</style>
